package com.jz.community.moduleshopping.confirmOrder.ui.view;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.confirmOrder.adapter.CommitOrderSendPriceListAdapter;
import com.jz.community.moduleshopping.confirmOrder.bean.OrderPostageInfo;
import com.jz.community.moduleshopping.goodsDetail.bean.GoodsPostageInfo;

/**
 * Created by Tony.Fan on 2018/8/15 09:14
 */
public class ShopSendPriceControl extends PopupWindow implements View.OnClickListener {

    private Activity mContext;
    private RecyclerView send_price_recyclerview;
    private TextView alter_dialog_cancel;
    private CommitOrderSendPriceListAdapter commitOrderSendPriceListAdapter;

    public ShopSendPriceControl(Activity context) {
        mContext = context;
        handleShow();
    }

    private void handleShow() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.shop_send_price_dialog_layout, null);
        this.setContentView(view);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        initView(view);
    }

    private void initView(View view) {
        send_price_recyclerview = view.findViewById(R.id.send_price_recyclerview);
        alter_dialog_cancel = view.findViewById(R.id.alter_dialog_cancel);
        alter_dialog_cancel.setOnClickListener(this);
        WindowManager.LayoutParams params = mContext.getWindow().getAttributes();
        params.alpha = 0.7f;
        setOnDismissListener(() -> {
            params.alpha = 1f;
            mContext.getWindow().setAttributes(params);
        });
        mContext.getWindow().setAttributes(params);
    }

    public void addShopPostageInfo(OrderPostageInfo basePostageInfo) {
        handleBindSendPriceListAdapter(basePostageInfo);
    }

    private void handleBindSendPriceListAdapter(OrderPostageInfo basePostageInfo) {
        if (!Preconditions.isNullOrEmpty(basePostageInfo)) {
            LinearLayoutManager main_layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
            send_price_recyclerview.setLayoutManager(main_layoutManager);
            commitOrderSendPriceListAdapter = new CommitOrderSendPriceListAdapter(basePostageInfo.getFeeList());
            commitOrderSendPriceListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//加载动画
            send_price_recyclerview.setAdapter(commitOrderSendPriceListAdapter);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.alter_dialog_cancel) {
            dismiss();
        }
    }
}
